package com.xpresso.utils.email;

import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;

import org.apache.log4j.Logger;

import com.xpresso.utils.exceptions.XpressoException;

/**
 * This class connects to a POP3 server to retrieve email messages.
 * There are three main methods connect(), checkMail() and close().
 * 
 * This class can be used as super class for other more specific 
 * processes that require to connect to a POP3 server.
 * 
 */
public class MailboxManager {

	Logger log = Logger.getLogger(getClass().getName());

	protected String host;
	protected String userName;
	protected String password;
	protected Folder folder;
	protected Store store;
	protected EmailProcessor emailProcessor;
	
	public MailboxManager(EmailProcessor emailProcessor){
		this.emailProcessor = emailProcessor;
		this.host  = emailProcessor.getEmailconf().getHost();
		this.userName = emailProcessor.getEmailconf().getUser();
		this.password = emailProcessor.getEmailconf().getPass();
	}	

	/**
	 * Gets the hostmaname to be used to connect
	 * @return String
	 */
	public String getHost() {
		return host;
	}

	/**
	 * Gets the password to be used to connect
	 * @return String
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * Gets the username to be used to connect
	 * @return String
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * Sets the hostmaname to be used to connect
	 * @param string
	 */
	public void setHost(String string) {
		host = string;
	}

	/**
	 * Sets the password to be used to connect
	 * @param string
	 */
	public void setPassword(String string) {
		password = string;
	}

	/**
	 * Sets the username to be used to connect
	 * @param string
	 */
	public void setUserName(String string) {
		userName = string;
	}

	/**
	 * Connects to the POP3 server set in the hostname property
	 * using the username and password provided.
	 * @param void
	 */
	public void connect()throws XpressoException{
		//Create empty properties
		Properties props = new Properties();
		// Get session
		Session session = Session.getDefaultInstance(props, null);
		
		try {
			// Get the Store
			store = session.getStore("pop3");
			store.connect(this.host, this.userName, this.password);
			// Get Folder
			folder = store.getFolder("INBOX");
			folder.open(Folder.READ_WRITE);
		} catch (NoSuchProviderException e) {
			log.error("An exception ocurred checking email: " + e);
			throw new XpressoException("An exception ocurred checking email", e);
		} catch (MessagingException e) {
			log.error("An exception ocurred checking email: " + e);
			throw new XpressoException("An exception ocurred checking email", e);
		} 		
	}

	/**
	 * Returns the available messages.
	 * @param void
	 * @throws MessagingException 
	 */
	public Message[] checkMail() throws MessagingException{
		return folder.getMessages();
	}

	/**
	 * Closes the connection with the POP3 server
	 * @param void
	 * @throws XpressoException 
	 */
	public void close() throws XpressoException{
		try {
			if (this.folder.isOpen()){
				this.folder.close(true);
				this.store.close();
			}
		} catch (MessagingException e) {
			throw new XpressoException("An exception ocurred closing folder", e);
		}
	}
}
